﻿// owner 蒋家伟

#include "Library.h"

// std
#include <fstream>
#include <string>
#include <cassert>

// qt
#include <QLibrary>

// jsdk
#include "StringUtils.h"
#include "IApplication.h"

using namespace jsdk;

Library & jsdk::Library::get()
{
    static jsdk::Library s_LibraryImp;
    return s_LibraryImp;
}

bool jsdk::Library::loadLibraries() const
{
    std::wifstream mycin;
    mycin.open(IApplication::applicationDirPath() + L"./Libraries.txt");

    if (!mycin.is_open())
    {
        assert(false && "Library configuration file failed to open !");
        return false;
    }

    std::wstring buf;
    QLibrary lib;
    while (getline(mycin, buf) && !buf.empty())
    {
#ifdef _WINDOWS
    #ifdef _DEBUG
        lib.setFileName(QString::fromStdWString(buf + L"D.dll"));
    #else
        lib.setFileName(QString::fromStdWString(buf + L".dll"));
    #endif
#elif defined (__linux__)
#ifdef _DEBUG
        lib.setFileName(QString::fromStdWString(buf + L"D.so"));
#else
        lib.setFileName(QString::fromStdWString(buf + L".so"));
#endif
#else
#error 其他操作系统暂未实现
#endif
        if (!lib.load())
        {
            std::string errorMsg = "library : " + StringUtils::toString(buf) + " a failed to load!";
            assert(false && errorMsg.c_str());
            mycin.close();
            return false;
        }
    }

    mycin.close();
    return true;
}
